package com.haoxi.gateway.runner.process;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.haoxi.gateway.common.INFOConst;
import com.haoxi.gateway.common.SQLConst;
import com.haoxi.gateway.common.SYSConst;
import com.haoxi.gateway.runner.RunnerMgr;
import com.haoxi.gateway.runner.smscommand.SubmitErrorMsg;
import com.haoxi.gateway.util.db.DBConnectUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.MessageFormat;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: QianXM
 * Date: 2006-4-3
 */
public final class ExceptionDateDeal {
    private static final Log log = LogFactory.getLog ( ExceptionDateDeal.class );

    /**
     * @param sendSmsCmdList
     * @return
     */
    public static boolean submitErrorDeal ( List errorSubmitList ) {
        boolean result = false;
        String dbName = (String)RunnerMgr.getInstance().getSystemConf().get(SYSConst.CONF_DB_GATEWAY);
        int listLength = errorSubmitList.size ();
        if ( 0 == listLength ) {
            log.error ( MessageFormat.format ( INFOConst.SKYINFO_CREATE_OBJECT_ERROR_STRING,
                                               new Object[]{"SendSmsCmd Object Array" , "ExceptionDateDeal.deleteErrorDate()"} ) );
            return result;
        }

        Connection conn = null;
        PreparedStatement pst = null;

        try {
            for ( int i = 0; i < listLength; i++ ) {
                SubmitErrorMsg submitErrorMsg = ( SubmitErrorMsg ) errorSubmitList.get ( i );
                conn = DBConnectUtil.open ( dbName );
                pst = conn.prepareStatement ( SQLConst.SAVE_HISTORY_ERROR_MSG );

                pst.setString ( 1, submitErrorMsg.getOrgMobile () );
                pst.setString ( 2, submitErrorMsg.getDestMobile () );
                pst.setString ( 3, submitErrorMsg.getFeeMobile () );
                pst.setInt ( 4, submitErrorMsg.getsTyep () );
                pst.setInt ( 5, submitErrorMsg.getMtFlag () );
                pst.setInt ( 6, submitErrorMsg.getReportFlag () );
                pst.setString ( 7, submitErrorMsg.getContent () );
                pst.setString ( 8, submitErrorMsg.getGatewayCode () );
                pst.setInt ( 9, submitErrorMsg.getProvince () );
                pst.setString ( 10, submitErrorMsg.getProvinceName () );
                pst.setInt ( 11, submitErrorMsg.getCityNumber () );
                pst.setString ( 12, submitErrorMsg.getCityName () );
                pst.setString ( 13, submitErrorMsg.getReserved () );
                pst.setInt ( 14, submitErrorMsg.getCp () );
                pst.setInt ( 15, submitErrorMsg.getFeeMobileType () );
                pst.setInt ( 16, submitErrorMsg.getDestMobileType () );
                pst.setInt ( 17, submitErrorMsg.getOrgMobileType () );
                pst.setInt ( 18, submitErrorMsg.getIsDeal () );
                pst.setInt ( 19, submitErrorMsg.getReSend () );
                pst.setString ( 20, submitErrorMsg.getTid () );
                pst.setString ( 21, submitErrorMsg.getErrorCode () );

                int num = pst.executeUpdate ();
                if ( 1 != num ) {
                    log.error ( "save error history msg unsuccessfully in ExceptionDateDeal.submitErrorDeal()!" );
                    log.error ( "the error history msg object is: " + submitErrorMsg.toString () );
                }
            }
        }
        catch ( SQLException sqle ) {
            log.error ( "SQLException in ExceptionDateDeal.submitErrorDeal()!", sqle );
        }
        finally {
            try {
                if ( null != pst ) {
                    pst.close ();
                }
                if ( null != conn ) {
                    conn.close ();
                }
            }
            catch ( SQLException sqle ) {
                log.error ( "release connection exception in ExceptionDateDeal.submitErrorDeal()!", sqle );
            }
        }

        return result;
    }
}
